package com.cu.employmentmanagementsystem.util;

import cn.dev33.satoken.stp.StpUtil;
import com.cu.employmentmanagementsystem.dao.StudentInformationDao;
import com.cu.employmentmanagementsystem.entity.*;
import com.cu.employmentmanagementsystem.service.AdminService;
import com.cu.employmentmanagementsystem.service.CompanyService;
import com.cu.employmentmanagementsystem.service.StudentInformationService;
import com.cu.employmentmanagementsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author CaptainUniverse_
 * @date 2024/7/1
 */
@Component
public class CommonUtils
{
    private static UserService userService;
    private static StudentInformationService studentInformationService;
    private static AdminService adminService;
    private static CompanyService companyService;

    @Autowired
    public void setUserService(UserService userService)
    {
        CommonUtils.userService = userService;
    }

    @Autowired
    public void setStudentInformationService(StudentInformationService studentInformationService)
    {
        CommonUtils.studentInformationService = studentInformationService;
    }

    @Autowired
    public void setAdminService(AdminService adminService)
    {
        CommonUtils.adminService = adminService;
    }

    @Autowired
    public void setCompanyService(CompanyService companyService)
    {
        CommonUtils.companyService = companyService;
    }

    public static Long getUserId()
    {
        User user = getUser();
        Long userId = user.getId();
        return userId;
    }

    public static Long getStudentId()
    {

        return getStudentInformation().getId();
    }

    public static Long getCompanyId()
    {
        return getCompany().getId();
    }

    public static Long getAdminId()
    {
        return getAdmin().getId();
    }

    public static Company getCompany()
    {
        Long userId = getUserId();
        Company company = new Company();
        company.setUserId(userId);
        List<Company> companyList = companyService.queryAllByCondition(company);
        return companyList.get(0);
    }

    public static String getUserRole()
    {
        return StpUtil.getRoleList().get(0);
    }

    public static StudentInformation getStudentInformation()
    {
        Long userId = getUserId();
        StudentInformation studentInformation = new StudentInformation();
        studentInformation.setUserId(userId);
        List<StudentInformation> studentInformationList = studentInformationService.queryAllByCondition(studentInformation);
        return studentInformationList.get(0);
    }

    public static Admin getAdmin()
    {
        Long userId = getUserId();
        Admin admin = new Admin();
        admin.setUserId(userId);
        List<Admin> adminList = adminService.queryAllByCondition(admin);
        return adminList.get(0);
    }

    public static User getUser()
    {
        String userAccount = StpUtil.getLoginIdAsString();
        User user = userService.getUserInfoByAccount(userAccount);
        return user;
    }

}
